//
//  RecordWork.m
//  K-Music
//
//  Created by apple on 16/1/17.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "RecordWork.h"
#import "InteractionCell.h"
#import "RecordWork_Medel.h"
#import "YYplayerVC.h"
@interface RecordWork ()<UITableViewDataSource,UITableViewDelegate>{
    RecordWork_Medel*aa;
}
@property(nonatomic,strong)NSMutableArray*allMusicData;
@property(nonatomic,strong)UITableView *mainTable;
@end

@implementation RecordWork

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我转发的作品";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"play" highIcon:@"play" target:self action:@selector(play_play)];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    [self get_record];
    
    aa=[[RecordWork_Medel alloc]init];
    
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,WIDTH , HEIGHT-64) style:(UITableViewStylePlain)];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;
    [self.view addSubview:_mainTable];
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)play_play
{
    YYplayerVC* yy = [[YYplayerVC alloc]init];
    
    yy.hidesBottomBarWhenPushed = YES;
    
    yy.musicData= [User_information conversion].musicData[0];
    
    [yy setTracks:[Track remoteTracks]];
    
    [self.navigationController pushViewController:yy animated:NO];
}

-(void)get_record{
    
    [MBProgressHUD showWithStatus:@"加载中..." toView:self.view];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString * userId = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:User_id]];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/records/user_id/%@/shared_records?page=0&size=20&sort=createdDate,DESC",userId];
        
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功GET --> %@", dic);
        NSArray *arr = [[dic objectForKey:@"body"] objectForKey:@"content"];
        _allMusicData = [NSMutableArray array];
        
        for (int i = 0; i<arr.count; i++) {
            [aa initDic:[[arr objectAtIndex:i] objectForKey:@"record"]];
            [_allMusicData addObject:aa];
        }
        
        [_mainTable reloadData];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showErrorWithStatus:@"数据获取失败" toView:self.view];
        
    }];
}

#pragma mark-----------------------UiTbaleView代理方法------------------------------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _allMusicData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 126;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString*inf=@"InteractionCell";
    
    InteractionCell*cell=[tableView  dequeueReusableCellWithIdentifier:inf];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"InteractionCell" owner:self options:nil ] firstObject];
        
    }
    
#warning 播放  暂时 用第一个用户DATA
    aa=_allMusicData[indexPath.row];
    cell.Itt_name.text=[NSString stringWithFormat:@"%@",aa.userId];
    cell.info_text.text=[NSString stringWithFormat:@"%@",aa.name];
    cell.infoter_text.text=[NSString stringWithFormat:@"%@",aa.descriptionD];
    
    cell.text1.text=[NSString stringWithFormat:@"%@",aa.totalPlay];
    cell.text2.text=[NSString stringWithFormat:@"%@",aa.totalGift];
    
    cell.text3.text=[NSString stringWithFormat:@"%@",aa.totalShare];
    
    cell.text4.text=[NSString stringWithFormat:@"%@",aa.totalPlayMonthly];
    
    cell.mm_text.text=[NSString stringWithFormat:@"%@",aa.size];
    [cell.image_photo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",aa.pictureUrl]]];
    cell.image_photo.layer.cornerRadius= cell.image_photo.width/2 ;
    cell.image_photo.layer.masksToBounds = YES;
    
    return cell;
}

@end
